package com.netsdk.demo.module;

import com.netsdk.lib.NetSDKLib;
import com.netsdk.lib.ToolKits;

import com.sun.jna.NativeLong;
import com.sun.jna.ptr.IntByReference;

/**
 * 抓图接口实现
 * 主要有 ：远程抓图、本地抓图、定时抓图功能
 */
public class CapturePictureModule {
	/**
	 * 本地抓图
	 */
	public static boolean localCapturePicture(NativeLong hPlayHandle, String picFileName) {
				
		if (!LoginModule.netsdk.CLIENT_CapturePictureEx(hPlayHandle, picFileName, NetSDKLib.NET_CAPTURE_FORMATS.NET_CAPTURE_JPEG)) { 
			System.err.printf("CLIENT_CapturePicture Failed!" + ToolKits.getErrorCodePrint());
			return false;
		} else { 
			System.out.println("CLIENT_CapturePicture success"); 
		}
		return true;
	}
	
	/**
	 * 远程抓图
	 */
	public static boolean remoteCapturePicture(int chn) {
		return snapPicture(chn, 0, 0);
	}
	
	/**
	 * 定时抓图
	 */
	public static boolean timerCapturePicture(int chn) {
		return snapPicture(chn, 1, 2);
	}
	
	/**
	 * 停止定时抓图
	 */
	public static boolean stopCapturePicture(int chn) {
		return snapPicture(chn, -1, 0);
	}
	
	/**
	 * 抓图
	 */
	private static boolean snapPicture(int chn, int mode, int interval) {
		// 发送抓图命令给前端设备，抓图的信息
		NetSDKLib.SNAP_PARAMS stuSnapParams = new NetSDKLib.SNAP_PARAMS(); 
		stuSnapParams.Channel = chn;  			// 抓图通道
		stuSnapParams.mode = mode;    			// 抓图模式
		stuSnapParams.Quality = 3;				// 画质
		stuSnapParams.InterSnap = interval; 	// 定时抓图时间间隔
		stuSnapParams.CmdSerial = 0;  			// 请求序列号，有效值范围 0~65535，超过范围会被截断为  
		
		IntByReference reserved = new IntByReference(0);
		if (!LoginModule.netsdk.CLIENT_SnapPictureEx(LoginModule.m_hLoginHandle, stuSnapParams, reserved)) { 
			System.err.printf("CLIENT_SnapPictureEx Failed!" + ToolKits.getErrorCodePrint());
			return false;
		} else { 
			System.out.println("CLIENT_SnapPictureEx success"); 
		}
		return true;
	}
	
	public static void setSnapRevCallBack(NetSDKLib.fSnapRev m_SnapReceiveCB){ 
		//设置抓图回调函数， 图片主要在m_SnapReceiveCB中返回
		LoginModule.netsdk.CLIENT_SetSnapRevCallBack(m_SnapReceiveCB, new NativeLong(0));
	}
}
